package com.gupao.nio;

import io.netty.buffer.ByteBuf;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.List;

/**
 * @基本功能:
 * @program:gupao-netty
 * @author: jiawy
 * @create:2024-10-10 19:12:32
 **/
public class NoBlockingServerT {
    public static List<SocketChannel> channelList = new ArrayList<>();
    public static void main(String[] args) throws Exception {


        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.configureBlocking(false);
        serverSocketChannel.socket().bind(new InetSocketAddress(8080));
        while (true) {
            SocketChannel socketChannel = serverSocketChannel.accept();

            if (socketChannel != null){
                socketChannel.configureBlocking(false);
                channelList.add(socketChannel);
            }else
            {
//                System.out.println("没有请求过来");
            }

            for (SocketChannel client: channelList){
                ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
                int num = client.read(byteBuffer);
                if (num > 0) {

                    System.out.println("客户端端口："+ client.socket().getPort()+",客户端收据："+new String(byteBuffer.array()));
                } else {
//                    System.out.println("等待客户端写数据");
                }
            }
        }
    }
}